package com.gistmap.ecepoi.common;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author zhangran
 * @date 2018/7/2
 */
@Component
public class ExcelUtils {


    @Autowired
    private ExcelService excelService;

    /**
     * web导出
     * @param os
     * @param objs
     * @param clz
     */
    public void export(OutputStream os, List objs, Class clz) {
        try {
            Workbook wb = excelService.handleExcel(objs, clz);
            wb.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 模板导出
     * @param os
     * @param column
     */
    public void getTemplate(OutputStream os, String[] column){
        try {
            Workbook wb = excelService.handleExcel(column);
            wb.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 流->List<T>
     * @param filePath
     * @param inputStream
     * @param clz
     * @param <T>
     * @return
     * @throws IOException
     */
    public <T> List<T> excel2Data(String filePath, InputStream inputStream, Class clz) throws IOException {
        return excelService.toObject(filePath , inputStream, clz);
    }


    public boolean isExcel(String filePath) {
        return filePath.matches("^.*\\.(?:xls)$")||filePath.matches("^.+\\.(?i)(xlsx)$");
    }
}
